<?php
/*
+ --------------------------------------------------- +
|  Ineo Article Manager - Version 2.x
|  Copyright 2006-2007, Dan Cryer and William McGann
+ --------------------------------------------------- +

Ineo Article Manager is free software; you can redistribute it and/or modify
it under the terms of version 3 of the GNU General Public License as published by
the Free Software Foundation.

Ineo Article Manager is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Ineo; see the file License.txt.  If not, write to
the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

*/

/**
* Ineo - Modules - Admin CP Overview
*
* @package		Ineo
* @subpackage   Modules
* @copyright	2006, Veriea
*/
class ineo_mod_acp_tools
{
	/**
	* Variable Setup
	*/
	var $ipsclass;
	var $core;
	
	/**
	* Initialize
	*/
	function module_init()
	{
		// ACP Module, Do nothing if not in ACP!
		if( !IN_ACP )
		{
			return false;
		}
	}
	
	/**
	* Default Function for this module:
	*/
	function module_default()
	{
		return $this->tools();
	}
	
	function tools()
	{
		$this->ipsclass->admin->page_title = 'Tools';
		$this->ipsclass->admin->page_detail = 'This page allows you to run certain management functions for Ineo, such as recounting article numbers on categories and recounting revisions made to articles.';
		
		if( !empty( $this->ipsclass->input['msg'] ) )
		{
			$this->core->api->box( 'System Message' , "<div style='padding: 5px'>{$this->ipsclass->input['msg']}</div>" );
			$this->ipsclass->html .= '<br />';
		}
		
		$this->tool_group( 
					'Topic Converter' , 
					$this->tool_entry( 'Convert Topic'  , 'Enter a topic ID or URL to create an article from it.' , "<form action='{$this->ipsclass->base_url}&section=components&act=ineo&module=tools&code=convert' method='post'>
<input type='text' style='width: 45%' name='topic' />".$this->core->api->build_dropdown(1,true)."<br /><input type='submit' value='Convert Topic' />
					</form>")
				  );
	}
		
	function tool_group( $title , $tools )
	{
		$group_html = "
		<table style='width: 100%' cellspacing='0'>
			{$tools}
		</table>
		";
		
		$this->core->api->box( $title , $group_html );
	}
	
	function tool_entry( $title , $desc , $code )
	{
		$tool = "
			<tr>
				<td class='".$this->core->api->row1."' style='padding: 5px'>
					<b>{$title}</b><br />
					{$desc}
				</td>
				<td class='".$this->core->api->row2."' style='padding: 5px; text-align: center;'>
					{$code}
				</td>
			</tr>
			";
		
		return $tool;
	}
	
	function convert()
	{
		$topic = $this->ipsclass->input['topic'];
		$id = intval( $topic );
		
		if( $id == 0 )
		{
			// They entered a url:
			$id = intval( preg_replace('/(.*)showtopic=([0-9]+)(.*)?/','$2',$topic) );
		}
		
		if( $id == 0 )
		{
			$this->ipsclass->boink_it($this->ipsclass->base_url . '&section=components&act=ineo&module=tools&msg=Tool Failed: No Topic ID Found');
		}
		
		$this->ipsclass->DB->simple_construct( array(
													'select' => 't.*,p.*',
													'from'   => array('topics' => 't'),
													'add_join' => array( 1 => array( 
																				
																				'type'  => 'left',
																				'from'  => array( 'posts' => 'p' ),
																				'where' => 'p.pid = t.topic_firstpost',
																		
																		)),
													'where' => 't.tid = ' . $id,
												) );
		$this->ipsclass->DB->simple_exec();
		
		$topic = $this->ipsclass->DB->fetch_row();
		
		$article = array(
							'article_title' => $topic['title'],
							'article_description' => $topic['description'],
							'article_content' => 0,
							'article_author' => $topic['starter_id'],
							'article_edited_by' => $topic['starter_id'],
							'article_created_date' => $topic['start_date'],
							'article_edited_date' => $topic['start_date'],
							'article_views' => $topic['views'],
							'article_revisions' => 1,
							'article_cat_id' => intval($this->ipsclass->input['category_parent']),
						);
		$this->ipsclass->DB->do_insert('ineo_articles',$article);
		
		$id = $this->ipsclass->DB->get_insert_id();
		
		$content = array( 'content_article' => $id , 'content_author' => $topic['starter_id'] , 'content_date' => $topic['start_date'] , 'content_content' => $topic['post'] );
		
		$this->ipsclass->DB->do_insert('ineo_content',$content);
		
		$content = $this->ipsclass->DB->get_insert_id();
		
		$this->ipsclass->DB->do_update('ineo_articles', array('article_content' => $content) , 'article_id = ' . $id );
		
		$this->ipsclass->DB->simple_construct( array('select' => 'attach_ext,attach_file,attach_location,attach_member_id,attach_filesize,attach_is_image', 'from' => 'attachments' , 'where' => 'attach_pid = ' . $topic['topic_firstpost']));
		$result = $this->ipsclass->DB->simple_exec();

		
		while( $res = $this->ipsclass->DB->fetch_row($result) )
		{
			$res['attach_article'] = $id;
			$res['attach_mid'] = $res['attach_member_id'];
			unset($res['attach_member_id']);
			$res['attach_size'] = $res['attach_filesize'];
			unset($res['attach_filesize']);
			$res['attach_isimage'] = $res['attach_is_image'];
			unset($res['attach_is_image']);
			
			$res['attach_location'] = $this->ipsclass->vars['upload_dir'] .'/'. $res['attach_location'];
			
			$this->ipsclass->DB->do_insert('ineo_attachments',$res);
		}
		
		$this->ipsclass->boink_it($this->ipsclass->base_url . '&section=components&act=ineo&module=tools&msg=Tool Completed: Article has been created!');
	}

}
?>